<?php
namespace app\index\controller;

use think\Controller;
use app\index\wechat\Wechat;

class Index extends Controller
{
    public $db;
    public $wc;
    public function __construct()
    {
        $this->db=model('Wxuser');
        $this->wc=new Wechat();
    }

    public function index($code='')
    {   
        $ac=$this->wc->getWebAccessToken($code);
        $userinfo=$this->wc->getUserInfo($ac);
        //添加这条数据前，判断一下又没有这个openid的数据
        $u=$this->db->where('openid',$userinfo['openid'])->find();
        if($u){
            $userinfo=$u;
        }else{
            $res=$this->db->data($userinfo)->allowField(true)->save();
            $userinfo['bonus']=5;
        }

        return view('index',['user'=>$userinfo]);
        // p($code);
    }

    public function chou($openid=''){
        $this->db->where('openid',$openid)->setDec('bonus');
    }
    public function cha($openid=''){
        $res=$this->db->where('openid',$openid)->value('bonus');
        echo $res;
    }

    public function indexpre($code='')
    {
        // $access_token=$this->wc->getWebAccessToken($code);
        // $userinfo=$this->wc->getUserInfo($access_token);
        // $u=$this->db->where('openid',$userinfo['openid'])->find();

        $openid='o-1ZVwxFIx2-NuXwfzFyi8JzLzZQ';
        $u=$this->db->where('openid',$openid)->find();
        // if($u){
        //     $this->db->allowField(true)->save($userinfo,$userinfo['openid']);
        // }else{
        //     $this->db->allowField(true)->save($userinfo);
        // }
        // return view('index',['user'=>$userinfo]);
        return view('index',['user'=>$u]);
    }
    public function test(){
        $user=model('User');
        $list=$user->all();
        // $this->assign('user',$list);
        return view('test',['user',$list]);
    }

    public function jspage(){
        $this->fetch();
    }

    public function lookmenu(){
        $wc=new Wechat();
        $url='https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$wc->getAccessToken();
        $res=curl($url);
        p($res);
    }

    public function createmenu(){
        $wc=new Wechat();

        $url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$wc->getAccessToken();
        // echo $url;
        // exit;
        $data=[
            "button"=>[
                [
                    "type"=>"click",
                    "name"=>"hello",
                    "key"=>"V1001_TODAY_MUSIC",
                ],
                [
                    "name"=>'menu',
                    "sub_button"=>[
                        [
                            "type"=>"view",
                            "name"=>"search",
                            "url"=>"http://www.soso.com/"
                        ],
                       [
                           "type"=>"view",
                           "name"=>"video",
                           "url"=>"http://v.qq.com/"
                       ],
                       [
                           "type"=>"click",
                           "name"=>"plaud",
                           "key"=>"V1001_GOOD"
                       ]
                    ]
                ]
            ]
        ];
        // echo json_encode($data);
        // exit;
        $res=curl($url,json_encode($data));
        p($res);
         // {
         //     "button":[
         //     {  
         //          "type":"click",
         //          "name":"今日歌曲",
         //          "key":"V1001_TODAY_MUSIC"
         //      },
         //      {
         //           "name":"菜单",
         //           "sub_button":[
         //           {    
         //               "type":"view",
         //               "name":"搜索",
         //               "url":"http://www.soso.com/"
         //            },
         //            {
         //               "type":"view",
         //               "name":"视频",
         //               "url":"http://v.qq.com/"
         //            },
         //            {
         //               "type":"click",
         //               "name":"赞一下我们",
         //               "key":"V1001_GOOD"
         //            }]
         //       }]
         // }
    }


    public function getCode(){
        $redirecturi=urlencode("http://wechat.jzcoder.com");
        // $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('appID').'&redirect_uri='.$redirecturi.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';

        $url2='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('appID').'&redirect_uri='.$redirecturi.'&response_type=code&scope=snsapi_userinfo&state=321#wechat_redirect';
        $this->redirect($url2);
    }

    public function getCode2(){
        $redirecturi=urlencode("http://wechat.jzcoder.com");
        // $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('appID').'&redirect_uri='.$redirecturi.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';

        $url2='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('appID').'&redirect_uri='.$redirecturi.'&response_type=code&scope=snsapi_userinfo&state=321#wechat_redirect';
        $this->redirect($url2);
    }


    public function getCodepre(){
    	$redirect_uri=urlencode("http://wechat.jzcoder.com");
    	$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('appID').'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';

    	$url2='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6851984ea35464ba&redirect_uri=http://wechat.jzcoder.com&response_type=code&scope=snsapi_userinfo&state=sdfsd#wechat_redirect';
    	
		// return $this->redirect($url);
		header('location:'.$url);
    	
    }
}
